/// <remarks>
/// ExtensionsManager Class
/// Extensions Class : contain the arraylist used to serialize the excluded extensions set
/// Extension Class  : extesion object
/// 
/// Authors: Georges Marceau, Matricis Informatique Inc.
///          Louis-Philippe Lebouthillier.
/// </remarks>

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;


namespace VcLib
{
    /// <summary>
    /// 
    /// </summary>
    public class ExtensionsManager
    {
        private const string fileConst= "\\excluded_extensions.xml";

        public ExtensionsManager()
        {
        }

        public void SaveToXML(IDictionary<string,bool> dics, string projectPath)
        {
            TextWriter twriter = null;

            try
            {
                Extensions exts = new Extensions();

                foreach (KeyValuePair<string, bool> pair in dics)
                {
                    exts.AddExtension(pair.Key, pair.Value);
                }

                //Serialize the class Extensions
                XmlSerializer xserializer = new XmlSerializer(typeof(Extensions));
                twriter = new StreamWriter(projectPath + fileConst);
                xserializer.Serialize(twriter, exts);
                twriter.Close();
            }
            catch
            {
                throw;
            }
            finally
            {
                if (twriter != null)
                    twriter.Close();
            }
        }

        public IDictionary<string,bool> LoadFromXML(string projectPath)
        {
            TextReader treader = null;
            
            try
            {
                //DeSerialize if xml exist
                //If the file doesnt exist it will return an empty Dictionnary
                IDictionary<string, bool> dics = new Dictionary<string, bool>();

                if (File.Exists(projectPath + fileConst))
                {
                    XmlSerializer xserializer = new XmlSerializer(typeof(Extensions));
                    treader = new StreamReader(projectPath + fileConst);
                    Extensions exts = (Extensions)xserializer.Deserialize(treader);
                    treader.Close();

                    int nbExt = exts.GetNbExtensions();

                    for (int i = 0; i < nbExt; i++)
                    {
                        Extension ext = exts.GetExtension(i);
                        dics.Add(ext.GetExtension(), ext.GetState());
                    }
                }
                return dics;
            }
            catch
            {
                throw;
            }
            finally
            {
                if (treader != null)
                    treader.Close();
            }
        }
    }

    /// <summary>
    /// Extensions Class
    /// this is the class who will be serialized
    /// Extension container
    /// </summary>
    /// 
    [Serializable]
    [XmlRoot("extensions")]
    public class Extensions
    {
        [XmlArray("Extensions")]
        [XmlArrayItem("extension", typeof(Extension))]
        public ArrayList arrayExtensions = new ArrayList();

        //Default constructor
        //Without its impossible to serialize
        public Extensions()
        {
        }

        public void AddExtension(string extension, bool state)
        {
            Extension ext = new Extension(extension, state);
            arrayExtensions.Add(ext);
        }

        public int GetNbExtensions()
        {
            return this.arrayExtensions.Count;
        }
        
        public Extension GetExtension(int index)
        {
            return ((Extension) this.arrayExtensions[index]);
        }
    }

    /// <summary>
    /// Extension Class
    /// Use to store atribute of one extension
    /// </summary>
    [Serializable]
    public class Extension
    {
        [XmlElement("ext")]
        public string extension;

        [XmlElement("state")]
        public bool state;

        public Extension() { }

        public Extension(string extension, bool state)
        {
            this.extension = extension;
            this.state = state;
        }

        public string GetExtension()
        {
            return this.extension;
        }

        public bool GetState()
        {
            return this.state;
        }
    }
}